package org.example.test;

import org.example.dao.ArticleDao;
import org.example.dao.CommentDao;
import org.example.entity.Article;
import org.example.entity.ArticleData;
import org.example.entity.Comment;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Date;
import java.util.Optional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-jpa.xml")
public class ManyToOne {


    @Autowired
    private CommentDao commentDao;

    @Autowired
    private ArticleDao articleDao;

    @Test
    public void save(){
        Optional<Article> optional = articleDao.findById(1);
        Article article = optional.get();
        Comment c1 = new Comment();
        c1.setArticle(article);
        c1.setComment("评论1");

        Comment c2 = new Comment();
        c2.setComment("评论2");
        c2.setArticle(article);

        try {

            commentDao.save(c1);
            commentDao.save(c2);
            System.out.println("保存成功");
        }catch (Exception e){
            System.out.println("保存失败");
            e.printStackTrace();
        }

    }



}
